import { createSlice, type  PayloadAction } from "@reduxjs/toolkit";
import { message } from "antd";
enum QuestionType {
  single = 'single',
  multiple = 'multiple',
  judge = 'judge',
  explain = 'Explain'
}

export interface Question {
  id:number
  type:QuestionType
  title:string
  list:any[]
}


interface State {
  examinationPaper:any[]
  questions: Question[]
}

const initialState: State = {
  // 试卷列表
  examinationPaper: [],
  // 创建试卷页面的题目列表
  questions:[
    // {
    //   id:1,
    //   title:'单选题',
    //   type:'single',
    // },
    // {
    //   id:2,
    //   title:'多选题',
    //   type:'multiple',
    // },
    // {
    //   id:3,
    //   title:'判断题',
    //   type:'judge',
    // }
  ]
}


const questionsSlice = createSlice({
  name:'questions',
  initialState,
  reducers:{
    addQuestion(state, action:PayloadAction<Omit<Question,'id'>>) {
      const index = state.questions.findIndex(v => v.type === action.payload.type)
      if(index === -1) {
        state.questions.push({
          id:Date.now(),
          ...action.payload,
          list:[]
        })
      } else {
        message.error(`${action.payload.title}已存在`)
      }
    },
    pushQuestion(state,action) {
      state.questions.forEach(item => {
        item.list = action.payload[item.type]
      })
    },
    delQuestion(state,action) {
      state.questions.splice(action.payload,1)
    },
    sortQuestion(state,action) {
      state.questions = action.payload
    }
  }
})

export const { addQuestion, pushQuestion, delQuestion, sortQuestion } = questionsSlice.actions

export default questionsSlice.reducer

